package com.yc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.entity.Admin;
import com.yc.mapper.AdminMapper;
import com.yc.service.IAdminService;
import com.yc.service.SendMailService;
import com.yc.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * InnoDB free: 4096 kB 前端控制器
 * </p>
 *
 * @author ly
 * @since 2022-06-06
 */
@Controller
@RequestMapping( "//admin" )
public class AdminController {
    @Resource
    private AdminMapper adminMapper;

    @Resource
    private IAdminService adminService;

    @Autowired
    private SendMailService sendMailService;

    @Autowired
    HttpSession session;

    @Resource
    JavaMailSenderImpl javaMailSender;

    //读取yml文件中username的值并赋值给form
    @Value( "${spring.mail.username}" )
    private String from;

    @RequestMapping( "login" )
    @ResponseBody
    public Result login(String name, String password) {
        Result result = new Result();
        List< Admin > list = adminService.login(name, password);
        if ( list == null ||list.size()==0 ) {
            result.setCode(0);
            result.setMsg("账号或密码错误，请确认后重试");
        } else if ( list.size() == 1 ) {
            if ( adminService.updateLastTime(list.get(0)) != 1 ) {
                result.setCode(0);
                result.setMsg("服务器出现错误，请联系管理员后重试");
            }
            result.setCode(1);
            result.setMsg("登陆成功");
        } else {
            result.setCode(0);
            result.setMsg("服务器出现错误，请联系管理员后重试");
        }
        return result;
    }

    @RequestMapping( "queryByAdname" )
    @ResponseBody
    public List< Admin > queryByAdname(String name) {
        return adminService.queryByAdname(name);
    }

    @RequestMapping( "updateAdminInfo" )
    @ResponseBody
    public Integer updateAdminInfo(@RequestBody List< Admin > adminList) {
        Admin admin = adminList.get(0);
        return adminService.updateAdminInfo(admin);
    }

    @RequestMapping( "updateAdminPwd" )
    @ResponseBody
    public Integer updateAdminPwd(@RequestBody List< Admin > adminList, @RequestParam String newpwd) {
        return adminService.updateAdminPwd(adminList.get(0), newpwd);
    }

    /**
     * 管理员通过邮箱找回密码
     *
     * @param map
     * @return
     */
    @PutMapping( "/findpass" )
    @ResponseBody
    public Result findpass(@RequestBody Map map) {
        Result result = new Result();
        String ucount = map.get("adcount").toString();
        String email = map.get("email").toString();
        String password = map.get("password2").toString();
        String code = map.get("code").toString();
        LambdaQueryWrapper< Admin > queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getAdcount, ucount);
        Admin admin = adminService.getOne(queryWrapper);
        if ( admin.getEmail().equals(email) && code.equals("") ) {
            sendMailService.sendMail(from, javaMailSender, email);
            result.setCode(1);
            result.setMsg("验证码发送成功");
        } else if ( !email.equals("") && !code.equals("") ) {
            if ( sendMailService.addUser(code).getCode() == 1 ) {
                adminMapper.modifypass(password, ucount);
                result.setCode(1);
                result.setMsg("找回密码成功");
            } else {
                result.setCode(0);
                result.setMsg("验证码不正确");
            }
        } else {
            result.setCode(0);
            result.setMsg("邮箱与当前账号绑定邮箱不一致");
        }
        return result;
    }
}

